/**
  ******************************************************************************
  * @file    Devices_E22.c
  * @author  AnshininTakaha
  * @version V1.21
  * @date		 2021/09/27
  * @brief   The File of E22(RF) Init Modify
	* @copyright (c) 2017-2021 JZI. All rights reserved.
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/
#include "Devices_E22.h"

#include "Devices_Command.h"
#include "Devices_Receiver.h"
#include "Devices_Frame.h"
#include "Devices_Battary.h"
#include "Devices_E22_BaseConfig.h"
#include "Devices_LED.h"
#include "Devices_Lidar.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/



/* Exported mention ----------------------------------------------------------*/
void E22_USART1_IT_Init(void);
void E22_USART_IT_Register(UART_HandleTypeDef* huartx);
void E22_RF_Setting(uint16_t Address,uint8_t Channel);
void E22_USART_ReBaudRate(uint32_t BaudRate);



/* Exported variables --------------------------------------------------------*/
/*E22 using Function*/
E22_Fuc E22_Function = E22_FucGroundInit;
#undef E22_FucGroundInit


/* Exported functions --------------------------------------------------------*/
/**
 * @brief USART1 Register init function of E22 Module.
 * @param void.
 * @return void.
 */
void E22_USART_IT_Register(UART_HandleTypeDef* huartx)
{
	/* Clear the Flag First for Prevent IDLE Flag Broken Init */
   __HAL_UART_CLEAR_IDLEFLAG(huartx);
	
	/* Open the Serial port Using Polling */
  __HAL_UART_ENABLE(huartx);
	
	/* Receive IT Enable: DMARx */
	__HAL_UART_ENABLE_IT(huartx,UART_IT_IDLE);
	
	/* Activation the first Receive */
	#ifdef LIDAR_USE_CODE
	HAL_UART_Receive_DMA(huartx, \
											(uint8_t*)LID_AT_CommandReceive, \
											AT_CommandLength);
	#endif
	#ifdef RECEIVE_USE_CODE
	/* Activation the first Receive */
	HAL_UART_Receive_DMA(huartx, \
											(uint8_t*)Receiver_LidarPusherReceive.Lidar_MsgReceive, \
											Lidar_MsgReceiveLength);
	#endif
	
	/* Transmit IT Enable: DMATx */
  //None..
	
	/* USART Register */
	E22_Function.E22_Register = huartx;
}



/**
 * @brief E22 Module setting function.
 * @param Address  The E22 Module confirm Address
 * @param Channel  The E22 Module confirm Channel
 * @attention The E22 Address & Channel must be the same.
							Both Transfer Node(s) and Receive Node(s).
 * @return void.
 */
void E22_RF_Setting(uint16_t Address,uint8_t Channel)
{
	/* Store for Reset & Setting Data */
	uint8_t Soft_Reset[6] = {0};
	/* Get Default Setting from Grand */
	E22_Setting_t E22Setting = {RF433_CONFIG_PACK};
	
	/* Set E22 Into Setting Mode */
	MD0_LOW();
	MD1_HIGH();
	
	/* Old board set mode */
	//DLC_MD0_LOW();
	//DLC_MD1_HIGH();
	HAL_Delay(100);
	
	/* Change Custom Modify Address & Channal */
	E22Setting.Address_L = (Address >> 8) & 0xFF;
	E22Setting.Address_H = (Address) & 0xFF;
	E22Setting.Channal = Channel % E22_CHANNEL_MAX;
	
	/* Send Data for Setting E22 */
	/* Then Waitting Transfer Complete */
	while(HAL_UART_Transmit_DMA(E22_Function.E22_Register, \
															E22Setting.E22_SettingMessage, \
															12) == HAL_BUSY);
	HAL_Delay(100);
	
	/* E22 Soft Reset */
	Soft_Reset[0] = 0xC4;
	Soft_Reset[1] = 0xC4;
	Soft_Reset[2] = 0xC4;
	
	/* Send Data for Setting E22 */
	/* Then Waitting Transfer Complete */
	while(HAL_UART_Transmit_DMA(E22_Function.E22_Register, \
															Soft_Reset, \
															3) == HAL_BUSY);
	HAL_Delay(100);
	
	/* Set E22 Into Working Mode */
	MD0_LOW();
	MD1_LOW();
	
	/* Old board set mode */
	//DLC_MD0_LOW();
	//DLC_MD1_LOW();
	
	HAL_Delay(100);
	
}



/**
 * @brief USART1 Reinitialization function of E22 Module(Working Baudrate).
 * @param void.
 * @return void.
 */
void E22_USART_ReBaudRate(uint32_t BaudRate)
{
	/* Stop the USART and Delink all the partner prevent usart Error */
	/* Keep the Instance first before deinit */
	USART_TypeDef *KeepInstance = E22_Function.E22_Register->Instance;

	/* Deinit */
	HAL_UART_DeInit(E22_Function.E22_Register);
	
	//ReSet BaudRate..
	huart1.Instance = KeepInstance;
  huart1.Init.BaudRate = BaudRate;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
	
	//Link the partner again..
  if (HAL_UART_Init(E22_Function.E22_Register) != HAL_OK)
  {
    Error_Handler();
  }
}







/* End -----------------------------------------------------------------------*/
